﻿#include "TurnTableConnectionStateManager.h"

#include "LogSaver.h"

TurnTableConnectionStateManager::TurnTableConnectionStateManager(QObject* parent):QObject(parent)
{
	connect(&timer_turnTableConnectionTimeOut, &QTimer::timeout, this, &TurnTableConnectionStateManager::slot_timer_turnTableConnectionTimeOut_timeOut);
	connect(&timer_checkThermostatTemperature, &QTimer::timeout, this, &TurnTableConnectionStateManager::slot_timer_checkThermostatTemperature_timeOut);
	connect(&timer_checkTurnTableCurrentState, &QTimer::timeout, this, &TurnTableConnectionStateManager::slot_timer_checkTurnTableCurrentState_timeOut);
	connect(&timer_checkTurnTableRunningState, &QTimer::timeout, this, &TurnTableConnectionStateManager::slot_timer_checkTurnTableRunningState_timeOut);
}

TurnTableConnectionStateManager::~TurnTableConnectionStateManager()
{}

void TurnTableConnectionStateManager::slot_timer_turnTableConnectionTimeOut_timeOut()
{
	logSaver->saveLog(QStringLiteral("%1：连接转台超时").arg(objectName()), LogSaver::Information);
	timer_turnTableConnectionTimeOut.stop();
	turnTableConnectionState = TurnTableCodec::State_Disconnected;
	emit signal_turnTable_connectionTimeOut();
	emit turnTableCodec->signal_turnTableConnectionState(TurnTableCodec::State_Disconnected);
}
void TurnTableConnectionStateManager::slot_timer_checkThermostatTemperature_timeOut()
{
	turnTableCodec->sendInstruction(TurnTableCodec::Thermostat_CurrentTemperature,0,0,0);
}
void TurnTableConnectionStateManager::slot_timer_checkTurnTableCurrentState_timeOut()
{
	turnTableCodec->sendInstruction(TurnTableCodec::TurnTable_CurrentState, 0, 0, 0);
}
void TurnTableConnectionStateManager::slot_timer_checkTurnTableRunningState_timeOut()
{
	turnTableCodec->sendInstruction(TurnTableCodec::TurnTable_RunningState, 0, 0, 0);
}
void TurnTableConnectionStateManager::slot_setTurnTableConnection(TurnTableCodec::ConnectionState connectionState)
{
	turnTableConnectionState = connectionState;
	switch (turnTableConnectionState)
	{
	case TurnTableCodec::State_Disconnected:
		timer_checkThermostatTemperature.stop();
		timer_checkTurnTableCurrentState.stop();
		timer_checkTurnTableRunningState.stop();
		break;
	case TurnTableCodec::State_Connected:
		timer_turnTableConnectionTimeOut.stop();
		timer_checkTurnTableCurrentState.start(interval_checkTurnTableCurrentState);
		timer_checkTurnTableRunningState.start(interval_checkTurnTableRunningState);
		break;
	case TurnTableCodec::State_Connecting:
		timer_turnTableConnectionTimeOut.start(interval_turnTableConnectionTimeOut);
		break;
	default:
		break;
	}
}

void TurnTableConnectionStateManager::slot_setThermostatConnection(TurnTableCodec::TurnTableInstructionFlag instructionFlag, float arg1, float arg2, float arg3)
{
	switch (instructionFlag)
	{
	case TurnTableCodec::Thermostat_Connect:
		thermostatConnectionState = TurnTableCodec::State_Connected;
		timer_checkThermostatTemperature.start(interval_checkThermostatTemperature);
		break;
	case TurnTableCodec::Thermostat_Disconnect:
		thermostatConnectionState = TurnTableCodec::State_Disconnected;
		timer_checkThermostatTemperature.stop();
		break;
	default:
		break;
	}
}

TurnTableCodec::ConnectionState TurnTableConnectionStateManager::getTurnTableConnectionState()
{
	return turnTableConnectionState;
}
TurnTableCodec::ConnectionState TurnTableConnectionStateManager::getThermostatConnectionState()
{
	return thermostatConnectionState;
}

int TurnTableConnectionStateManager::getInterval_turnTableConnectionTimeOut()
{
	return interval_turnTableConnectionTimeOut;
}
void TurnTableConnectionStateManager::setInterval_turnTableConnectionTimeOut(int interval_turnTableConnectionTimeOut)
{
	this->interval_turnTableConnectionTimeOut = interval_turnTableConnectionTimeOut;
}
int TurnTableConnectionStateManager::getInterval_checkThermostatTemperature()
{
	return interval_checkThermostatTemperature;
}
void TurnTableConnectionStateManager::setInterval_checkThermostatTemperature(int interval_checkThermostatTemperature)
{
	this->interval_checkThermostatTemperature = interval_checkThermostatTemperature;
}
int TurnTableConnectionStateManager::getInterval_checkTurnTableCurrentState()
{
	return interval_checkTurnTableCurrentState;
}
void TurnTableConnectionStateManager::setInterval_checkTurnTableCurrentState(int interval_checkTurnTableCurrentState)
{
	this->interval_checkTurnTableCurrentState = interval_checkTurnTableCurrentState;
}
int TurnTableConnectionStateManager::getInterval_checkTurnTableRunningState()
{
	return interval_checkTurnTableRunningState;
}
void TurnTableConnectionStateManager::setInterval_checkTurnTableRunningState(int interval_checkTurnTableRunningState)
{
	this->interval_checkTurnTableRunningState = interval_checkTurnTableRunningState;
}
TurnTableCodec* TurnTableConnectionStateManager::getTurnTableCodec()
{
	return turnTableCodec;
}
void TurnTableConnectionStateManager::setTurnTableCodec(TurnTableCodec* turnTableCodec)
{
	if (Q_NULLPTR!=this->turnTableCodec)
	{
		disconnect(this->turnTableCodec, &TurnTableCodec::signal_turnTableConnectionState, this, &TurnTableConnectionStateManager::slot_setTurnTableConnection);
		disconnect(this->turnTableCodec, &TurnTableCodec::signal_instructionEcho, this, &TurnTableConnectionStateManager::slot_setThermostatConnection);
	}
	this->turnTableCodec = turnTableCodec;
	connect(this->turnTableCodec, &TurnTableCodec::signal_turnTableConnectionState, this, &TurnTableConnectionStateManager::slot_setTurnTableConnection);
	connect(this->turnTableCodec, &TurnTableCodec::signal_instructionEcho, this, &TurnTableConnectionStateManager::slot_setThermostatConnection);
}
LogSaver* TurnTableConnectionStateManager::getLogSaver()
{
	return logSaver;
}
void TurnTableConnectionStateManager::setLogSaver(LogSaver* logSaver)
{
	this->logSaver = logSaver;
}
